package com.ruoyi.web.controller.util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysStorage;
import com.ruoyi.system.service.ISysStorageService;

@RestController
@RequestMapping("qiniukodo")
public class QiniuKodoController {

	@Resource
	private QiniuUtils qiniuKodoUtil;
	
    @Autowired
    private ISysStorageService sysStorageService;

    /**
     * 上传七牛云文件
     * @param localFilePath
     * @param file
     * @return
     * @throws Exception
     */
	@Anonymous
	@RequestMapping("upload")
	public AjaxResult upload(String localFilePath, MultipartFile file) throws Exception {
		String FilePath = "";
			// 上传文件路径
			String filePath = RuoYiConfig.getUploadPath();
			// 上传并返回新文件名称
			String fileName = FileUploadUtils.upload(filePath, file);
			String getExtension=FileUploadUtils.getExtension(file);
			AjaxResult ajax = AjaxResult.success();
			ajax.put("fileName", fileName);
//			ajax.put("newFileName", FileUtils.getName(fileName));
//			ajax.put("originalFilename", file.getOriginalFilename());

			QiniuKodoController qiniuKodo = new QiniuKodoController();
			String fileurl = qiniuKodo.extractString(fileName);
//			String subString = s.replace("/", "\\");
			String url = (filePath + "/" + fileurl).replace("/", "\\");
			FilePath = qiniuKodoUtil.upload(url);
			String filesize="";
			File f= new File(url);
			if (f.exists() && f.isFile()){
				filesize=f.length() / 1024 + "KB";
				System.out.println("文件" + f.getName() + "的大小是：" + f.length() / 1024 + "KB");
			}
			String getFileUrl = qiniuKodoUtil.getFileUrl(FilePath);
			System.out.println("FilePath:"+FilePath);
			SysStorage sysStorage=new SysStorage();
			sysStorage.setFilename(FilePath);
			sysStorage.setType(getExtension);
			sysStorage.setUrl(getFileUrl);
			sysStorage.setSize(filesize);
			sysStorage.setChannel("0");
			sysStorageService.insertSysStorage(sysStorage);
			ajax.put("fileNameUrl", getFileUrl);
			return ajax;

//		return AjaxResult.success(localFilePath, FilePath);

	}
	/**
	 * 查询七牛云所有文件
	 */
	@Anonymous
	@RequestMapping("listSpaceFiles")
	public void listSpaceFiles() {
		qiniuKodoUtil.listSpaceFiles();
	}
	/**
	 *  获取下载文件的链接
	 * @param fileName
	 * @return
	 */
	@Anonymous
	@RequestMapping("getFileUrl")
	public AjaxResult getFileUrl(String fileName) {
		String getFileUrl = "";
		try {
			getFileUrl = qiniuKodoUtil.getFileUrl(fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return AjaxResult.success(fileName, getFileUrl);
	}
	/**
	 * 批量删除七牛云空间中的文件
	 * @param fileList
	 */
	@Anonymous
	@RequestMapping("deleteFile")
	public void deleteFile(String[] fileList) {
		qiniuKodoUtil.deleteFile(fileList);
	}

	public static void main(String[] args) {
		QiniuKodoController s = new QiniuKodoController();
		// F:/ruoyi/uploadPath/upload/2022/11/06/宝信通话记录重复插入故障分析.docx
		String a = s.extractString("/profile/upload/2022/11/06/宝信通话记录重复插入故障分析.docx");
		System.out.println(a);
	}

	private String extractString(String s) {
		for (int i = 0; i < 3; i++) {
			s = s.substring(s.indexOf("/") + 1);
		}
		String subString = s.replace("/", "\\");
		return subString;
	}
}
